package controller
{
	import common.SharedObjectUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import model.MainModel;
	
	public class RssController extends EventDispatcher
	{
		private var mainModel:MainModel;
		private var loader:URLLoader;
		private var timer:Timer;

		public static const BAMBOO_URL_NOT_SET:String = "BAMBOO_URL_NOT_SET";
		public static const FAILS:String = "FAILURE";
		public static const SUCCESS:String = "SUCCESSFUL";
		public static const FAILURE_DETECTION:String = "FAILURE_DETECTION";
		public static const SUCCESS_DETECTION:String = "SUCCESS_DETECTION";
		public static const UPDATE:String = "UPDATE";
		
		private var sharedObject:SharedObjectUtil = SharedObjectUtil.getInstance();

		public function RssController(mainModel:MainModel) {
			this.mainModel = mainModel;
			 
			if( sharedObject.refreshRateInMillis <= 0 ) { 
				sharedObject.refreshRateInMillis = 1000*60;
			}
			timer = new Timer(sharedObject.refreshRateInMillis);
			timer.addEventListener(TimerEvent.TIMER, fetchRss);
			timer.start();
		}
		
		public function fetchRss(event:Event = null):void { 
			if( sharedObject.bambooUrl != null && sharedObject.bambooUrl != "" ) { 
				var urlRequest:URLRequest = new URLRequest(sharedObject.bambooUrl);
				loader = new URLLoader();
				loader.addEventListener(Event.COMPLETE, onRssLoaded);
				loader.addEventListener(IOErrorEvent.IO_ERROR, function():void { 
					dispatchEvent(new Event(BAMBOO_URL_NOT_SET));
				});
				loader.load(urlRequest);	
			}else{
				dispatchEvent(new Event(BAMBOO_URL_NOT_SET));
			}
		}

		private function onRssLoaded(event:Event):void { 
			mainModel.globalFailure = false;
			mainModel.latests.removeAll();
			var xml:XML;
			try { 
				xml = new XML(loader.data);
			}catch(e:Error){ 
				trace('url specified is not valid xml');
				dispatchEvent(new Event(BAMBOO_URL_NOT_SET));
				return;
			}
			var items:XMLList = xml.channel.item;
			for each( var item:XML in items ) { 
				var planKey:String = getPlanKey(item.title);
				
				var found:Boolean = false;
				for each( var itemFromLatests:XML in mainModel.latests ) { 
					if( getPlanKey(itemFromLatests.title) == planKey ) { 
						found = true;
						break;
					}
				}
				if( !found ) { 
					mainModel.globalFailure ||= fetchState(item.title) == FAILS; 
					mainModel.latests.addItem(item);
					if( mainModel.latests.length == 5 ) { 
						break;
					}
				}
			}
			if( mainModel.globalFailure ) { 
				dispatchEvent(new Event(FAILURE_DETECTION));
			}else{
				dispatchEvent(new Event(SUCCESS_DETECTION));
			}
			dispatchEvent(new Event(UPDATE));
		}

		public function getPlanKey(title:String):String { 
			return title.substring(0, title.indexOf("-", title.indexOf("-")+1));
		}
		public function getBuildNumber(title:String):String { 
			return title.match( "-([1-9][0-9]*) " )[1];
		}
		
		public function fetchState(title:String):String { 
			if( title.indexOf(SUCCESS) > 0 ) { 
				return SUCCESS;
			}
			return FAILS;
		}
	}
}